package cd.safe.school.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import cd.safe.school.bean.Dept;
import cd.safe.school.bean.PatriarchShare;
import cd.safe.school.bean.SchoolNew;
import cd.safe.school.bean.SharePager;
import cd.safe.school.bean.ShareReply;

public class Parse {
	// 解析分享信息
	public static List<PatriarchShare> patriarchparse(int urlid) {
		String url = "http://125.64.132.84:8080/jxt/app/share/dt/" + urlid
				+ ".json";
		List<PatriarchShare> lists = new ArrayList<PatriarchShare>();
		try {
			URL u = new URL(url);
			URLConnection conn = u.openConnection();
			InputStream in = conn.getInputStream();
			String ret = NetTool.getContent(in, "utf-8");
			// System.out.println(ret + ";;;;;;;;;;;");
			JSONObject obj = new JSONObject(ret);
			JSONObject result = obj.getJSONObject("result");
			JSONArray list = result.getJSONArray("list");
			String code = result.getString("code");
			for (int i = 0; i < list.length(); i++) {
				JSONObject share = list.getJSONObject(i);
				PatriarchShare pshare = new PatriarchShare();
				int id = share.getInt("id");
				String title = share.getString("title");
				long publishTime = share.getLong("publishTime");
				int userId = share.getInt("userId");
				int canReply = share.getInt("canReply");
				int status = share.getInt("status");
				String sysflag = share.getString("sysflag");
				String postcon = share.getString("postcon");
				pshare.setId(id);
				pshare.setUserId(userId);
				pshare.setCanReply(canReply);
				pshare.setPostcon(postcon);
				pshare.setTitle(title);
				pshare.setStatus(status);
				pshare.setSysflag(sysflag);
				pshare.setPublishTime(publishTime);
				// System.out.println(pshare);
				lists.add(pshare);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return lists;

	}

	// 解析分页查询信息
	// infoType代表信息类型:1听写英语，2优+作文，3代表同步解析 4易错题库
	public static List<SharePager> parsepage(int infoType, int page,
			int pageSize) {
		String url = "http://125.64.132.84:8080/jxt/app/res/list/" + infoType
				+ "/" + page + "/" + pageSize + ".json";
		List<SharePager> lists = new ArrayList<SharePager>();
		try {
			URL u = new URL(url);
			URLConnection conn = u.openConnection();
			InputStream in = conn.getInputStream();
			String content = NetTool.getContent(in, "utf-8");

			JSONObject res = new JSONObject(content);
			JSONObject result = res.getJSONObject("result");
			int rowCount = result.getInt("rowCount");
			int pageCount = result.getInt("pageCount");
			int rowCountPerPage = result.getInt("rowCountPerPage");
			int curPageNum = result.getInt("curPageNum");
			JSONArray list = result.getJSONArray("result");
			for (int i = 0; i < list.length(); i++) {
				SharePager sp = new SharePager();
				JSONObject js = list.getJSONObject(i);
				int id = js.getInt("id");
				String sendid = js.getString("sendid");
				String receiveid = js.getString("receiveid");
				String filetype = js.getString("filetype");
				int type = js.getInt("type");
				int canReply = js.getInt("canreply");
				int sysflag = js.getInt("sysflag");
				int ischecked = js.getInt("ischecked");
				String ur = js.getString("url");
				String title = js.getString("title");
				String description = js.getString("description");
				sp.setUrl(ur);
				sp.setTitle(title);
				sp.setDescription(description);
				sp.setId(id);
				sp.setSendid(sendid);
				sp.setReceiveid(receiveid);
				sp.setFiletype(filetype);
				sp.setType(type);
				sp.setCanReply(canReply);

				sp.setSysflag(sysflag);

				sp.setIschecked(ischecked);
				// System.out.println(sp);
				lists.add(sp);

			}
		} catch (Exception e) {

			e.printStackTrace();
		}
		return lists;

	}

	// 校园动态
	public static List<SchoolNew> parseschool(int page, int pageSize) {
		String url = "http://125.64.132.84:8080/jxt/app/news/list/" + page
				+ "/" + pageSize + ".json";
		List<SchoolNew> lists = new ArrayList<SchoolNew>();
		try {
			URL u = new URL(url);
			URLConnection conn = u.openConnection();
			InputStream in = conn.getInputStream();
			String ret = NetTool.getContent(in, "utf-8");
			JSONObject obj = new JSONObject(ret);
			JSONObject result = obj.getJSONObject("result");
			int code = result.getInt("code");
			JSONObject list = result.getJSONObject("list");
			int rowCount = list.getInt("rowCount");
			int pageCount = list.getInt("pageCount");
			int rowCountPerPage = list.getInt("rowCountPerPage");
			int curPageNum = list.getInt("curPageNum");
			JSONArray re = list.getJSONArray("result");
			for (int i = 0; i < re.length(); i++) {
				JSONObject js = re.getJSONObject(i);
				SchoolNew sn = new SchoolNew();
				int idl = js.getInt("idl");
				String title = js.getString("title");
				long time = js.getLong("time");
				int publishman = js.getInt("publishman");
				String sysflag = js.getString("sysflag");
				String content = js.getString("content");
				sn.setIdl(idl);
				sn.setContent(content);
				sn.setTime(time);
				sn.setTitle(title);
				sn.setPublishman(publishman);
				sn.setSysflag(sysflag);
				// System.out.println(sn);
				lists.add(sn);

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return lists;

	}

	// 根据分享ID查询回复
	public static List<ShareReply> parsereply(int urlid, int page, int pageSize) {
		String url = "http://125.64.132.84:8080/jxt/app/reply/" + urlid + "/"
				+ page + "/" + pageSize + ".json";
		List<ShareReply> lists = new ArrayList<ShareReply>();
		try {
			URL u = new URL(url);
			URLConnection conn = u.openConnection();
			InputStream in = conn.getInputStream();
			String ret = NetTool.getContent(in, "utf-8");
			JSONObject obj = new JSONObject(ret);
			JSONObject result = obj.getJSONObject("result");
			int code = result.getInt("code");
			JSONObject list = result.getJSONObject("list");
			int rowCount = list.getInt("rowCount");
			int pageCount = list.getInt("pageCount");
			int rowCountPerPage = list.getInt("rowCountPerPage");
			int curPageNum = list.getInt("curPageNum");
			JSONArray re = list.getJSONArray("result");
			for (int i = 0; i < re.length(); i++) {
				ShareReply sr = new ShareReply();
				JSONObject js = re.getJSONObject(i);
				int id = js.getInt("id");
				int userId = js.getInt("userId");
				int shareId = js.getInt("shareId");
				long replyTime = js.getLong("replyTime");
				int isverify = js.getInt("isverify");
				String reply = js.getString("reply");
				String sysflag = js.getString("sysflag");
				sr.setId(id);
				sr.setUserId(userId);
				sr.setShareId(shareId);
				sr.setIsverify(isverify);
				sr.setReply(reply);
				sr.setSysflag(sysflag);
				sr.setReplyTime(replyTime);
				System.out.println(sr);
				lists.add(sr);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return lists;

	}

	// 获取登录返回信息
	public static int parselogin(String parameter) {
		String url = "http://222.221.6.248:9588/login.aspx?mobile=" + parameter;
		int code = -100;
		try {
			URL u = new URL(url);
			URLConnection conn = u.openConnection();
			InputStream in = conn.getInputStream();
			String ret = NetTool.getContent(in, "utf-8");
			JSONObject jb = new JSONObject(ret);
			JSONObject result = jb.getJSONObject("result");
			code = result.getInt("code");
			if (code == 1001) {
				JSONArray list0 = result.getJSONArray("list0");
				for (int i = 0; i < list0.length(); i++) {
					JSONObject job = list0.getJSONObject(i);
					String userid = job.getString("userid");
					String username = job.getString("username");
					String loginid = job.getString("loginid");
					String usertype = job.getString("usertype");
					String mobileclient = job.getString("mobileclient");
					String mobiledate = job.getString("mobiledate");
					User.action = usertype;
					User.phone = parameter;
					User.userID = userid;

				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return code;

	}

	// 查询学校 年级 班级
	public static List<ListItemData> parseschool(String parentid) {
		String url = "http://222.221.6.248:9588/DeptQuery.aspx?parentid="
				+ parentid + "&userid=" + User.userID;
		List<ListItemData> des = new ArrayList<ListItemData>();
		try {
			URL u = new URL(url);
			URLConnection conn = u.openConnection();
			InputStream in = conn.getInputStream();
			String ret = NetTool.getContent(in, "utf-8");
			JSONObject j = new JSONObject(ret);
			JSONObject js = j.getJSONObject("result");
			int code = js.getInt("code");
			if (code == 1001) {
				JSONArray ja = js.getJSONArray("list0");
				for (int i = 0; i < ja.length(); i++) {
					JSONObject job = ja.getJSONObject(i);

					ListItemData d = new ListItemData();
					String deptid = job.getString("deptid");
					String deptname = job.getString("deptname");
					String depttype = job.getString("depttype");
					String pareid = job.getString("parentid");
					String remark = job.getString("remark");
					String issend = job.getString("issend");
					d.setDeptid(deptid);
					d.setDeptname(deptname);
					d.setDepttype(depttype);
					d.setIssend(issend);
					d.setRemark(remark);
					d.setParentid(pareid);
					des.add(d);

				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return des;

	}

}
